const jwt = require("jsonwebtoken");
const jwtScrect = "zyy"; //签名

//登录接口 生成token的方法
const setToken = function (username, password) {
  return new Promise((resolve, reject) => {
    const token = jwt.sign({ username:username, password:password }, jwtScrect, {
      expiresIn: "2h",
    });
    resolve(token);
  });
};
//各个接口需要验证token的方法
const getToken = function (token) {
  return new Promise((resolve, reject) => {
    if (!token) {
      reject({
        error: "token空",
      });
    } else {
      const info = jwt.verify(token.split(' ')[1], jwtScrect);
      resolve(info); 
    }
  });
};

module.exports = {
  setToken,
  getToken,
};
